﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Web;
using System.IO;
using System.Text;
using Common;


//1、拍照请求：
//http://192.168.121.248:82/hik_robot/camera/get_image   
//请求参数
//device = 设备ip

//2、AI识别请求：
//(钢胚识别)http://192.168.121.248:81/code/track_number
//(卷芯架识别)http://192.168.121.248:81/code/track_shelf_number
//(C型钩识别)http://192.168.121.248:81/code/track_hanger_number
//请求参数
//imageUrl = 图片地址 也就是上面拍照请求返回的oss地址
//  range = 识别范围(可选参数)
//rotate=旋转角度(可选参数)


/*
 * 打标机
 */

namespace DingApi.Controllers
{
    [RoutePrefix("api/Dbj")]
    public class DbjController : ApiController
    {

        string ps_name = "PostGetPicNum";

        int i = get_order.get_order_num();



        [HttpPost]
        [Route("PostGetPicNum")]
        public System.Data.DataSet PostGetPicNum([FromBody]  JObject obj)
        {
            DateTime dt0 = DateTime.Now;
            DateTime dt1 = DateTime.Now;
            TimeSpan timeDifference;
            int subTime = 0;


            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables[0].TableName = "SYS";
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Columns.Add("NUM");



            ClassMyFile.WriteLog("begin-------------------------------- i=["+i.ToString()+ "],dt_last=["+ get_order.get_dt_last().ToString("yyyy-MM-dd HH:mm:ss")+ "]", ps_name);

            //逻辑校验；


            timeDifference = DateTime.Now - get_order.get_dt_last();
            subTime = timeDifference.Hours * 3600 + timeDifference.Minutes * 60 + timeDifference.Seconds;

            if (subTime <= 10)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "请求间隔不得少于8秒！", "-1");
                return outBlock;
            }



         




            dt1 = DateTime.Now;
            ClassMyFile.WriteLog("get_imageUrl begin--------------------------------  ", ps_name);

            string imageUrl = ClassL2Ai.get_imageUrl("10.233.121.18");


            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            ClassMyFile.WriteLog(string.Format("get_imageUrl end--------------------------------总耗时{0}ms, imageUrl=[{1}]", subTime, imageUrl), ps_name);
            dt1 = DateTime.Now;


            if (imageUrl == "-1")
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "摄像头抓拍异常！","-1");
                return outBlock;
            }


            ClassMyFile.WriteLog("get_pic_ocr begin--------------------------------  ", ps_name);
            string str_num = ClassL2Ai.get_imageOcr(imageUrl, "C");


            if (str_num == "-1")
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "摄像头抓拍异常2！", "-1");
                return outBlock;
            }

            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            ClassMyFile.WriteLog(string.Format("get_pic_ocr end--------------------------------总耗时{0}ms ,str_num=[{1}]", subTime, str_num), ps_name);
            dt1 = DateTime.Now;


         


            //等待时间
            System.Threading.Thread.Sleep(1800);


            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            ClassMyFile.WriteLog(string.Format("接口等待 end--------------------------------总耗时{0}ms  ", subTime), ps_name);
            dt1 = DateTime.Now;
             


            timeDifference = DateTime.Now - dt0;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            ClassMyFile.WriteLog(string.Format("end--------------------------------总耗时{0}ms  ", subTime), ps_name);


            outBlock.Tables["SYS"].Rows.Add(0, "", str_num);

            get_order.set_dt_last();

            return outBlock;

        }



        [HttpPost]
        [Route("PostGetPicNum2")]
        public System.Data.DataSet PostGetPicNum2([FromBody]  JObject obj)
        {
            DateTime dt0 = DateTime.Now;
            DateTime dt1 = DateTime.Now;
            TimeSpan timeDifference;
            int subTime = 0;

            string ps_name = "PostGetPicNum2";

            ClassMyFile.WriteLog("begin-------------------------------- ", ps_name);


            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables[0].TableName = "SYS";
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Columns.Add("NUM");


            string ip = obj["ip"] == null ? "10.233.121.3" : obj["ip"].ToString().Trim();
            string type = obj["type"] == null ? "C" : obj["type"].ToString().Trim();



           



            dt1 = DateTime.Now;
            ClassMyFile.WriteLog("get_imageUrl begin--------------------------------  ", ps_name);

            string imageUrl = ClassL2Ai.get_imageUrl(ip);


            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            ClassMyFile.WriteLog(string.Format("get_imageUrl end--------------------------------总耗时{0}ms, imageUrl=[{1}]", subTime, imageUrl), ps_name);
            dt1 = DateTime.Now;


            if (imageUrl == "-1")
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "摄像头抓拍异常！", "-1");
                return outBlock;
            }


            ClassMyFile.WriteLog("get_pic_ocr begin--------------------------------  ", ps_name);
            string str_num = ClassL2Ai.get_imageOcr(imageUrl, "C");


            if (str_num == "-1")
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "识别异常！", "-1");
                return outBlock;
            }

            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            ClassMyFile.WriteLog(string.Format("get_pic_ocr end--------------------------------总耗时{0}ms ,str_num=[{1}]", subTime, str_num), ps_name);
            dt1 = DateTime.Now;


             
 
 

            timeDifference = DateTime.Now - dt0;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            ClassMyFile.WriteLog(string.Format("end--------------------------------总耗时{0}ms  ", subTime), ps_name);


            outBlock.Tables["SYS"].Rows.Add(0, "", str_num);

           

            return outBlock;

        }



        [HttpPost]
        [Route("PostGetOcr")]
        public System.Data.DataSet PostGetOcr([FromBody]  JObject obj)
        {
            DateTime dt0 = DateTime.Now;
            DateTime dt1 = DateTime.Now;
            TimeSpan timeDifference;
            int subTime = 0;


            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables[0].TableName = "SYS";
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Columns.Add("NUM");



            ClassMyFile.WriteLog("begin-------------------------------- i=[" + i.ToString() + "]", ps_name);

            //逻辑校验；

         

            string imageUrl      = obj["imageUrl"] == null ? "2024-04 -13/IP_10.233.121.3/IMG_1712980143487.jpg" : obj["imageUrl"].ToString().Trim();
            string type         = obj["type"] == null ? "C" : obj["type"].ToString().Trim();


            imageUrl = "http://192.168.11.120:9000/material-tracking/upload/ai_camera/" + imageUrl;
            

            ClassMyFile.WriteLog("imageUrl  --------------------------------  "+ imageUrl, ps_name);

            ClassMyFile.WriteLog("type --------------------------------  "+ type, ps_name);
            ClassMyFile.WriteLog("get_pic_ocr begin--------------------------------  ", ps_name);




            string str_num = ClassL2Ai.get_imageOcr(imageUrl, type);  
            if (str_num == "-1")
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "识别错误！", "-1");
                return outBlock;
            }

            timeDifference = DateTime.Now - dt1;
            subTime = (timeDifference.Hours * 3600 * 1000) + (timeDifference.Minutes * 60 * 1000) + (timeDifference.Seconds * 1000) + timeDifference.Milliseconds;
            ClassMyFile.WriteLog(string.Format("get_pic_ocr end--------------------------------总耗时{0}ms ,str_num=[{1}]", subTime, str_num), ps_name);
            dt1 = DateTime.Now;

            

            outBlock.Tables["SYS"].Rows.Add(0, "", str_num);

            get_order.set_dt_last();

            return outBlock;

        }




 


         




    }



   
    
}